// pages/create/create.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgId: '',
    motionID: 1,
    show:false,//控制下拉列表的显示隐藏，false隐藏、true显示
    displayyangli: 'block',
    selectData:['1','2','3','4','5','6'],//下拉列表的数据
    select1: 9,
    index:1,//选择的下拉列表下标
    imagedisplay:'none',
    imagemsg: '',
    btndisplayS: '',
    btndisplayC: '',
    imageUrl: '',
    arr: ["1", "2"],
    fileList: [],  //文件上传时 在前端展示上传的图片
    newArr:[],   //点击删除按钮的时候 进行赋值
    img_name: [],
    displayA: 'none',
    resData: '',
    yxbvalue: 0,
    ydbvalue: 81.69,
    zxbvalue: 0,
    zdbvalue: 97,
    yjvalue: 0,
    yxtvalue: 80,
    zjvalue: 0,
    zxtvalue: 0,
    ydtvalue: 0,
    zdtvalue: 0,
    bzvalue: 0,
    pictures: '',
    ip: '',
    pose: '',
    motionGroups: [],
    motions: [],
    motion_ids: [],
    newMotions: '',
    selectedGroupIndex: 0,
    selectedGroup: '',
    selectedMotionIndex: 0,
    selectedMotion: '',
    selectedMotionId: '',
    currentTag: '',
    color: '',
    border: '',
    imgSrc: '',
    p1: '',
    p2: ''
    },
    change: function (){
      this.setData({
        selectedMotionId: ''
      })
      this.onLoad()
    },
  // chooseImage() {
  //   var app = getApp()
  //   var ip = app.globalData.ServerAddress
  //   let that = this
  //   wx.chooseImage({
  //     count: 1, // 可选择的图片数量
  //     sizeType: ['compressed'], // 压缩图片
  //     sourceType: ['album', 'camera'], // 来源：相册或相机
  //     success:  (res)=> {
  //       // 将选择的图片上传到服务器
  //       that.setData({
  //         imageUrl: res.tempFilePaths[0],
  //         imagedisplay:'',
  //         imagemsg: res.tempFilePaths[0]
  //       })
  //       this.uploadImage(res.tempFilePaths[0]);
  //     }
  //   })
  // },
  getData: function (e){
    
    console.log(e.currentTarget.dataset.text)
    let frist = e.currentTarget.dataset.text[0]
    let second = e.currentTarget.dataset.text[1]
    let current = e.currentTarget.dataset.text[2]
    this.setData({
      currentTag: current,
      selectedMotionId : this.data.motion_ids[frist][second]
    })
  },
  submitdata: function(e){
    var app = getApp()
    var ip = app.globalData.ServerAddress2
    console.log(e.detail.value)
   
    let that = this
    let first = this.data.index + 1
    console.log(first)
    let s = this.data.selectData[first]
    let imagemsg = this.data.imagemsg
    console.log(s)
    console.log(imagemsg)
    //在这发送请求
    // console.log(this.data.index)
    // this.setData({
    //   select1:e.detail.value.select1
    // })
    console.log(first)
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: "http://" + ip + "/currency/alphapose_api",
      method: 'POST',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data: {
        "pose_id": this.data.selectedMotionId,
        "data": this.data.img_name
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        that.setData({
          resData: res.data.得分,
          displayA: 'block',
          yxbvalue: res.data.result.得分.右小臂,
          ydbvalue: res.data.result.得分.右大臂,
          zxbvalue: res.data.result.得分.左小臂,
          zdbvalue: res.data.result.得分.左大臂,
          yjvalue: res.data.result.得分.右脚,
          yxtvalue: res.data.result.得分.右小腿,
          zjvalue: res.data.result.得分.左脚,
          zxtvalue: res.data.result.得分.左小腿,
          ydtvalue: res.data.result.得分.右大腿,
          zdtvalue: res.data.result.得分.左大腿,
          pictures: res.data.pictures,
          p1: res.data.pictures[0],
          p2: res.data.pictures[1]
        })
      },
      complete:function(res) {
        
        // wx.navigateBack({
        //   delta: 1
        // });
      },
    });
  },
  chooseImage() {
     
    var app = getApp()
    var ip = app.globalData.ServerAddress2
    let that = this
    wx.chooseImage({
      count: 1, // 可选择的图片数量
      sizeType: ['compressed'], // 压缩图片
      sourceType: ['album', 'camera'], // 来源：相册或相机
      success:  (res)=> {
        // 将选择的图片上传到服务器
        that.setData({
          imageUrl: res.tempFilePaths[0],
          imagedisplay:'inline-block',
          displayyangli: 'inline-block',
          imagemsg: res.tempFilePaths[0]
        })
        this.uploadImage(res.tempFilePaths[0]);
      }
    })
  },
  uploadImage(imagePath) {
    wx.showLoading({
      title: '请稍后...',
    })
    let arr = this.data.newArr
    var app = getApp()
    var ip = app.globalData.ServerAddress2
    let that = this
    wx.uploadFile({   //这里一定要用  wx.uploadFile 否则无法穿到后台
      filePath: imagePath, //你要上传的路径
      name: 'data',   //你上传到后台的name值
      formData:{    // 如果你要验证你的token 可以用formData来给后台传值
      },
      url: "http://" + ip + "/currency/media",
      success(res){
        let img = res.data.slice(35,res.data.length-2)
        var data = JSON.parse (res.data)
       img =  data.picture_name

        console.log(res)
        arr.push(img);   //返回图片的路径  并追加到新数组里面
        that.setData({
          newArr:arr,   //在这里重新赋值，用来做删除
          img_name: img
        })

      },
      complete: () => {
        //隐藏loading效果
        wx.hideLoading()
      }
    })
    // wx.uploadFile({
    //   url: "http://" + ip + "/post/media",
    //   filePath: imagePath, // 图片文件路径
    //   name: 'data', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
    //   success: (res) => {
    //     // 上传成功后，将服务器返回的图片地址更新到image标签中
    //   },
    //   fail: function (res) {
    //     console.log(res);
    //   }
    // })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(this.data.arr)
    var app = getApp()
    var ip = app.globalData.ServerAddress
    this.setData({
      ip: ip
    })
    let that = this
    let arr0
    wx.request({
      url: "http://" + ip + "/currency/alphapose_api",
      method: 'GET',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data: {
        
      },
      success: function (res) {
        console.log(res)
        console.log(res.data.data[1])
        let arr0=[]
        that.setData({
          selectData: res.data.data,
        })
        console.log(that.data.selectData)
        for(let i = 1; res.data.data[i]!=null; i++){
          console.log(res.data.data[i])
          arr0.push(res.data.data[i])
        }
        that.setData({
          arr: arr0
        })
        console.log(that.data.arr)
      },
     
    }),
    wx.request({
      url: "http://" + ip + "/currency/alphapose_api",
      method: 'GET',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        //  data = res.data
        let data = res.data
        let poses = res.data.data.poses
        let motiongroups = Object.values(data.data.pose_groups)
        let motionDataArray = Object.values(data.data.poses).map(group => group.map(item => item.name))
        let motionIdDataArray = Object.values(data.data.poses).map(group => group.map(item => item.id))
        that.setData({
          motionGroups: motiongroups,
          motions: motionDataArray,
          motion_ids: motionIdDataArray,
          poses: poses
        });
      },
    })
  },
  
  // pickerChange: function (e) {
  //   const value = e.detail.value;
  //   const selectedGroupIndex = value[0];
  //   const selectedMotionIndex = value[1];
  //   const selectedGroup = this.data.motionGroups[selectedGroupIndex];
  //   const selectedMotion = this.data.motions[selectedGroupIndex][selectedMotionIndex];

  //   this.setData({
  //     selectedGroupIndex: selectedGroupIndex,
  //     selectedGroup: selectedGroup,
  //     selectedMotionIndex: selectedMotionIndex,
  //     selectedMotion: selectedMotion,
  //     selectedMotionId: this.data.motion_ids[selectedGroupIndex][selectedMotionIndex]
  //   });

  //   // console.log("用户选择的运动组别为：" + this.data.selectedGroup);
  //   // console.log("用户选择的运动项目为：" + this.data.selectedMotion);
  //   // console.log("用户选择的运动项目ID为：" + this.data.selectedMotionId)
  // },
  // // 点击下拉显示框
  // selectTap(){
  //   console.log(1)
  // this.setData({
  //  show: !this.data.show
  // });
  // },
  // // 点击下拉列表
  // optionTap(e){
  // let Index=e.currentTarget.dataset.index
  // console.log(Index);//获取点击的下拉列表的下标
  // this.setData({
  //  index:Index,
  //  show:!this.data.show,
  //  imgId: Index+1,
  //  motionID: Index + 1
  // });
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }

})